const app = getApp();
const util = require('../../utils/util');
const api = require('../../utils/api');
const Page = require('../../utils/pageProxy');

Page({
    data: {
        id: '',
        specialPageInfo: '',
        specialPageDetail: '',

        productList: [],
        pageNum: 1,
        pageSize: 8,
        total: 0,
        noContent: false, //无数据
        isFinished: false, //加载结束
    },
    onLoad(options) {
        this.setData({
            id: options.id,
        });
        this.getSpecialPageDetail();
        this.getSpecialPageProduct();
    },
    onShow() {

    },

    getSpecialPageDetail() { //专题页信息
        api.post('/product/showcase/topic?topicId=' + this.data.id)
            .then(res => {
                this.setData({
                    specialPageInfo: res,
                    specialPageDetail: res.content ? util.formatRichText(res.content) : '',
                });
            })
    },
    getSpecialPageProduct() { //专题页商品
        let params = {
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize
        }
        api.post(`/product/showcase/page?moduleNo=` + this.data.id, params)
            .then(res => {
                let list = res.content;
                let newList = [...this.data.productList, ...list];
                this.setData({
                    productList: newList,
                    total: res.total
                });

                //是否无内容标识
                if(this.data.productList.length == 0) {
                    this.setData({
                        noContent: true
                    })
                } else {
                    this.setData({
                        noContent: false
                    });
                }
                //是否加载完毕标识
                if(this.data.productList.length >= this.data.total) {
                    this.setData({
                        isFinished: true
                    })
                } else {
                    this.setData({
                        isFinished: false
                    })
                }
                wx.stopPullDownRefresh();
            })
    },
    viewProductDetail(e) { //查看商品详情
        let item = e.currentTarget.dataset.item;
        wx.navigateTo({
            url: `/pages/productDetail/productDetail?id=${item.skuSaleId}&shopId=${item.shopId}`,
        });
    },

    onPullDownRefresh() {
        //下拉刷新
        this.setData({
            productList: [],
            pageNum: 1,
            total: 0,
            isFinished: false,
        });
        this.getSpecialPageProduct();
    },
    onReachBottom() {
        //加载更多
        if(!this.data.isFinished) {
            this.setData({
                pageNum: this.data.pageNum+1,
            });
            
            setTimeout(() => {
                this.getSpecialPageProduct();
            }, 800);
        }
    },
    onShareAppMessage() {
        return {
            title: this.data.specialPageInfo.title,
            desc: this.data.specialPageInfo.title,
            path: 'pages/specialPage/specialPage?id=' + this.data.id, // 路径，传递参数到指定页面
        }
    }
})